package com.maigao.service;

import com.maigao.entity.form.AddStudentCourseForm;
import com.maigao.entity.jpa.Student;
import com.maigao.entity.jpa.StudentCourse;
import com.maigao.entity.vo.StudentCourseVO;
import com.maigao.persistent.StudentCourseRepository;
import com.maigao.persistent.StudentRepository;
import com.maigao.util.DateUtils;
import org.apache.commons.lang3.StringUtils;
import org.apache.commons.lang3.tuple.Pair;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.math.BigDecimal;
import java.time.LocalDateTime;
import java.util.ArrayList;
import java.util.List;

/**
 * @program: maigao
 * @description:
 * @author: yongza
 * @create: 2024-10-21 23:01
 **/
@Service
public class StudentCourseService {
    @Autowired
    private StudentCourseRepository studentCourseRepository;
    @Autowired
    private StudentRepository studentRepository;

    public List<StudentCourseVO> tryFindAll() {
        List<StudentCourse> studentCourses = studentCourseRepository.findAll();
        return convert2Detail(studentCourses);
    }

    public List<StudentCourseVO> tryFindAllByStudent(Integer studentId) {
        List<StudentCourse> studentCourses = studentCourseRepository.findByStudentId(studentId);
        return convert2Detail(studentCourses);
    }

    private List<StudentCourseVO> convert2Detail(List<StudentCourse> studentCourses) {
        List<StudentCourseVO> studentCourseVOS = new ArrayList<>();
        for (StudentCourse studentCourse : studentCourses) {
            StudentCourseVO item = new StudentCourseVO();
            BeanUtils.copyProperties(studentCourse, item);
            item.setCreateTime(DateUtils.format(studentCourse.getCreateTime(), DateUtils.Formatter.DATETIME_YYYY_MM_DD_HH_MM_SS));
            item.setLastUpdateTime(DateUtils.format(studentCourse.getLastUpdateTime(), DateUtils.Formatter.DATETIME_YYYY_MM_DD_HH_MM_SS));
            studentCourseVOS.add(item);
        }
        return studentCourseVOS;
    }

    public Pair<Boolean, String> tryAddStudentCourse(AddStudentCourseForm addStudentCourseForm) {
        Integer studentId = addStudentCourseForm.getStudentId();
        String studentName = addStudentCourseForm.getStudentName();
        Integer giftCount = null == addStudentCourseForm.getGiftCount() ? 0 : addStudentCourseForm.getGiftCount();
        Integer totalCount = addStudentCourseForm.getTotalCount();
        String courseType = addStudentCourseForm.getCourseType();
        String totalPrice = addStudentCourseForm.getTotalPrice();
        String unitPrice = addStudentCourseForm.getUnitPrice();
        if (StringUtils.isAnyBlank(courseType, studentName, totalPrice, unitPrice) || null == studentId || null == totalCount) {
            return Pair.of(false, "参数不完整!");
        }
        Student student = studentRepository.findById(studentId);
        if (null == student) {
            return Pair.of(false, "学生不存在!");
        }
        if (!StringUtils.equals(student.getStudentName(), studentName)) {
            return Pair.of(false, "学生姓名错误!");
        }
        StudentCourse studentCourse = new StudentCourse();
        studentCourse.setStudentId(studentId);
        studentCourse.setStudentName(studentName);
        studentCourse.setTotalCount(totalCount);
        studentCourse.setRemainCount(totalCount + giftCount);
        studentCourse.setGiftCount(giftCount);
        studentCourse.setCourseType(courseType);
        studentCourse.setTotalPrice(new BigDecimal(totalPrice));
        studentCourse.setUnitPrice(new BigDecimal(unitPrice));
        studentCourse.setCreateTime(LocalDateTime.now());
        studentCourse.setCourseNote(addStudentCourseForm.getCourseNote());
        studentCourse.setStatus(StudentCourse.Status.NORMAL.getValue());
        studentCourseRepository.save(studentCourse);
        return Pair.of(true, "");
    }


    public Pair<Boolean, String> tryUpdateStatus(Integer id, Integer newStatus) {
        StudentCourse studentCourse = studentCourseRepository.findById(id);
        if (null == studentCourse) {
            return Pair.of(false, "学生课程不存在!");
        }
        StudentCourse.Status statusEnum = StudentCourse.Status.getByValue(newStatus);
        if (null == statusEnum) {
            return Pair.of(false, "状态非法!");
        }
        studentCourseRepository.updateStudentCourseStatus(id, newStatus);
        return Pair.of(true, "");
    }

    public Pair<Boolean, String> decreaseRemainCount(Integer studentCourseId, Integer classHour) {
        StudentCourse studentCourse = studentCourseRepository.findById(studentCourseId);
        if (null == studentCourse) {
            return Pair.of(false, "学生课程不存在!");
        }
        int remainCount = studentCourse.getRemainCount();
        if (remainCount >= classHour) {
            studentCourse.setRemainCount(remainCount - classHour);
            studentCourseRepository.save(studentCourse);
            return Pair.of(true, "");
        } else {
            return Pair.of(false, "剩余课时小于代扣课时, 操作失败!");
        }
    }
}
